package com.wqtz.chechai.view.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.wqtz.chechai.R;
import com.wqtz.chechai.adapter.CarSeriesAdapter;
import com.wqtz.chechai.api.Constans;
import com.wqtz.chechai.base.BaseActivity;
import com.wqtz.chechai.bean.SeriesBean;
import com.wqtz.chechai.presenter.BasePresent;
import com.wqtz.chechai.utils.JsonUtils;
import com.wqtz.chechai.utils.NetworkUtils;
import com.wqtz.chechai.utils.ToastUtils;

import org.w3c.dom.ls.LSException;

import java.security.spec.PSSParameterSpec;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 项目名称： CheChai
 * 创建人 : Liberation
 * 创建时间: 2016/12/28 15:39
 */

public class CarSeriesActivity extends BaseActivity implements NetworkUtils.GetJson, CarSeriesAdapter.BigOnClick {
	CarSeriesAdapter mCarSeriesAdapter;
	List<SeriesBean.ResultBean.PinpaiListBean> list;
	@BindView(R.id.iv_back)
	ImageView mIvBack;
	@BindView(R.id.tv_title)
	TextView mTvTitle;
	@BindView(R.id.iv_car_img)
	ImageView mIvCarImg;
	@BindView(R.id.tv_carbrand)
	TextView mTvCarbrand;
	@BindView(R.id.iv_del_return)
	ImageView mIvDelReturn;
	@BindView(R.id.ll_car_model)
	LinearLayout mLlCarModel;
	@BindView(R.id.rv_carSeries)
	RecyclerView rv_carSeries;
	private String url;
	private String json;
	private String brand;
	private SeriesBean seriesBean;
	private String carBrand; //车名
	private String carSeriesName; //车系

	@Override
	public void init() {
		ButterKnife.bind(this);
		Intent intent = getIntent();
		brand = intent.getStringExtra("brand");
		carBrand = intent.getStringExtra("carBrand");
		url = String.format(Constans.CAT_SERIES, brand);
		list = new ArrayList<>();
		NetworkUtils.doHttpAsync(url, this, this);
	}

	@Override
	public void loadData() {
		mTvCarbrand.setText(carBrand);

	}

	@Override
	public int setContentViews() {
		return R.layout.activity_carseries;
	}

	@Override
	public BasePresent createPresenter() {
		return null;
	}


	@OnClick({R.id.iv_back, R.id.iv_del_return})
	public void onClick(View view) {
		switch (view.getId()) {
			case R.id.iv_back:
				finish();
				break;
			case R.id.iv_del_return:
				finish();
				break;
		}
	}

	@Override
	public void getJsonCallBack(String result, String request_url) {
		if (url.equals(request_url) && result != null)
			seriesBean = new SeriesBean();
		    seriesBean = JsonUtils.getCarSeries(result);
		    Logger.e(seriesBean.toString());
		    list.addAll(seriesBean.getResult().getPinpai_list()) ;
		    rv_carSeries.setLayoutManager(new LinearLayoutManager(this));
		    mCarSeriesAdapter = new CarSeriesAdapter(R.layout.item_carseries_first,list,this);
		    rv_carSeries.setAdapter(mCarSeriesAdapter);
		    mCarSeriesAdapter.setBigClick(this);
	}

	@Override
	public void bigClick(int positon, List<SeriesBean.ResultBean.PinpaiListBean.XilieBean> list) {
		//ToastUtils.show(this,list.get(positon).getXlname(),1000);
		Intent intent = new Intent(this,CarSeriesDetailActivity.class);
		intent.putExtra("xmid",list.get(positon).getXlid());
		intent.putExtra("carseriesName",carSeriesName+"-"+list.get(positon).getXlname());
		intent.putExtra("name",list.get(positon).getXlname());
	     intent.putExtra("carBrand",carBrand);
		startActivityForResult(intent,1005);
	}

	@Override
	public void outClick(String carSeriesName, List<SeriesBean.ResultBean.PinpaiListBean> data) {
		this.carSeriesName = carSeriesName;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(1005==requestCode&&resultCode==RESULT_OK){
			Intent intent = new Intent();
			intent.putExtra("cxname",data.getStringExtra("cxname"));
			intent.putExtra("id",data.getStringExtra("id"));
			intent.putExtra("price",data.getStringExtra("price"));
			setResult(RESULT_OK,intent);
			finish();
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		activitys.add(this); //添加当前的activity
	}
}
